#include <sys/types.h>
#include <wait.h>
#include <unistd.h>
#include <signal.h>

#include <stdio.h>
#include <stdlib.h>

void mywait(int *s){
    while((*s)<0);
    (*s)--;

}

void mysignal(int *s){
    (*s)--;
}

typedef void (*sighandler_t) (int);
void sigcat()
{
printf("%d Process continue\n",getpid());
}

int main()
{
    int pid=-1,pid2=-1;
    
    char *ls_argv[]={"/bin/ls","-al", NULL};
    char *ps_argv[]={"/bin/ps","-a", NULL};

    pid2 = fork();//创建执行ls的

    int ps_status,ls_status;
    
    signal(SIGINT,(sighandler_t)sigcat);
    if(pid2 != 0){
        printf("create thread 2\n");
        pid = fork();//在主线程中创建执行ps的
    }

    if(pid2 == 0)//执行ls
    {
        pause();
        printf("im ls thread");
        ls_status = execve("/bin/ls",ls_argv,NULL);
    }
    
    if(pid == 0)//执行ps
    {
        printf("im ps thread\n");
        ps_status = execve("/bin/ps",ps_argv,NULL);
    }

    
    if(pid != 0 && pid2 != 0){//主线程


        printf("im main\n");
        
        waitpid(pid,&ps_status,0);
        kill(pid2,SIGINT);
        
        printf("next step,excu thread2.\n");
        waitpid(pid2,&ls_status,0);  

        printf("over\n");
    }
    return 0;
}


//注意,execve执行后的代码不会被执行.
